//
//  GMEnclosureVC.m
//  Teecloud
//
//  Created by xhkj on 2018/11/24.
//  Copyright © 2018 小志. All rights reserved.
//

#import "GMEnclosureVC.h"
#import "GMEnclosureCell.h"
#import "GMAttachListModel.h"
#import "ASProgressPopUpView.h"
#import "GMLookPDFVC.h"

@interface GMEnclosureVC ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tabView;
@property (weak, nonatomic) IBOutlet ASProgressPopUpView *progressView;

@end

@implementation GMEnclosureVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabView registerNib:[UINib nibWithNibName:@"GMEnclosureCell" bundle:nil] forCellReuseIdentifier:@"GMEnclosureCell"];
    self.progressView.popUpViewCornerRadius = 12.0;
    self.progressView.font = [UIFont systemFontOfSize:12];
    [self.progressView showPopUpViewAnimated:YES];
    self.progressView.hidden = YES;
    self.progressView.progress =0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GMEnclosureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"GMEnclosureCell" forIndexPath:indexPath];
    GMAttachListModel *model= self.dataArr[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GMAttachListModel *model= self.dataArr[indexPath.row];

    if ([model.FileType isEqualToNumber:@(8)]||[model.FileType isEqualToNumber:@(9)]) {
        //如果是rar或者zip直接下载
        NSArray *array = [model.Url componentsSeparatedByString:@"/"];
        NSString *fileName =[array lastObject];
        self.progressView.hidden =NO;
        [HJNetwork downloadWithURL:model.Url fileDir:TENYUNDOWN fileName:fileName progress:^(NSProgress *progress) {
            CGFloat currentpros = 100.0*progress.completedUnitCount/progress.totalUnitCount;
            [self.progressView setProgress:currentpros animated:YES];
        } callback:^(NSString *path, NSError *error) {
            if (!error) {
                self.progressView.hidden =YES;
                [MBProgressHUD showErrorOrSuc:@"下载完成" toView:self.view];
            }else{
                self.progressView.hidden =YES;
                [MBProgressHUD showErrorOrSuc:@"下载失败" toView:self.view];
            }
          
        }];
    }else{
        //跳转到阅读页面
        UIViewController * parent = [self.view containingViewController];
        if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
            [parent dismissSemiModalView];
        }
        self.encolseureBlock(model);
    }
}
- (IBAction)closePopView:(id)sender {
    UIViewController * parent = [self.view containingViewController];
    if ([parent respondsToSelector:@selector(dismissSemiModalView)]) {
        [parent dismissSemiModalView];
    }

    [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoPlay" object:nil];
}


@end
